package com.Bean.Dao;


import com.Bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDaoImpl implements StudentDao{

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int inster() {
        //修改 添加 删除
        String sql = "insert into student(number,name,grade,clbum,gender) values (?,?,?,?,?)";
        int i = jdbcTemplate.update(sql,
                new Object[]{"202303", "奥特曼","2023级", "计算机科学与技术", "男"});

        return i;

    }
//根据姓名更新学生消息记录
    @Override
    public int update(String name) {
        String sql = "update student set name = ? where number = ?";
        int a = jdbcTemplate.update(sql,
                new Object[]{"", name});
        return a;
    }
    //根据学号查询单个学生记录
    @Override
    public Student selectOne(String number) {
        String sql = "select * from student where number = ?";
        Student student = jdbcTemplate.queryForObject(sql,
                new BeanPropertyRowMapper<Student>(Student.class),
                new Object[]{number});
        return student;
    }

    //根据学号删除学生记录
    @Override
    public int delete(String number){
        String sql = "delete from student where number = ?";
        int b = jdbcTemplate.update(sql,
                new Object[]{number});
        return b;
    }

    @Override
    public List<Student> selectAll() {
        String sql = "select * from student";
        List<Student> list = jdbcTemplate
                .query(sql,new BeanPropertyRowMapper<Student>
                        (Student.class)
                );
//        jdbcTemplate.queryForList(sql, new BeanPropertyRowMapper<Student>(Student.class));

        return list;
    }
}
